/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.suli.wcs.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.annotation.Log;
import me.zhengjie.annotation.rest.AnonymousPostMapping;
import me.zhengjie.suli.wcs.domain.AgvTaskDetail;
import me.zhengjie.suli.wcs.domain.AgvTasks;
import me.zhengjie.suli.wcs.repository.AgvTaskDetailRepository;
import me.zhengjie.suli.wcs.service.AgvTasksService;
import me.zhengjie.suli.wcs.service.dto.AgvTasksQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
* @website https://eladmin.vip
* @author Allen Gao
* @date 2024-02-04
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "AGV任务管理")
@RequestMapping("/api/agvTasks")
@Slf4j
public class AgvTasksController {

    private final AgvTasksService agvTasksService;
    private final AgvTaskDetailRepository agvTaskDetailRepository;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('agvTasks:list')")
    public void exportAgvTasks(HttpServletResponse response, AgvTasksQueryCriteria criteria) throws IOException {
        agvTasksService.download(agvTasksService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询AGV任务")
    @ApiOperation("查询AGV任务")
    @PreAuthorize("@el.check('agvTasks:list')")
    public ResponseEntity<Object> queryAgvTasks(AgvTasksQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(agvTasksService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增AGV任务")
    @ApiOperation("新增AGV任务")
    @PreAuthorize("@el.check('agvTasks:add')")
    public ResponseEntity<Object> createAgvTasks(@Validated @RequestBody AgvTasks resources){
        return new ResponseEntity<>(agvTasksService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改AGV任务")
    @ApiOperation("修改AGV任务")
    @PreAuthorize("@el.check('agvTasks:edit')")
    public ResponseEntity<Object> updateAgvTasks(@Validated @RequestBody AgvTasks resources){
        agvTasksService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除AGV任务")
    @ApiOperation("删除AGV任务")
    @PreAuthorize("@el.check('agvTasks:del')")
    public ResponseEntity<Object> deleteAgvTasks(@RequestBody String[] ids) {
        for (String id : ids) {
            List<AgvTaskDetail> agvTaskDetailList = agvTaskDetailRepository.getAgvTaskDetailByMainId(id);
            agvTaskDetailRepository.deleteAll(agvTaskDetailList);
        }
        agvTasksService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping("/send-task-toAgv")
    @Log("发送任务至AGV")
    @ApiOperation("发送任务至AGV")
    public String sendToAgv(@RequestBody String ids) {
        return agvTasksService.sendToAgv(ids);
    }


    @AnonymousPostMapping("/agvCallBack")
    @Log("AGV任务状态回调")
    @ApiOperation("AGV任务状态回调")
    public ResponseEntity<Object> agvCallBackForStatus(@RequestBody String param) {
        log.info("AGV任务回调返回："+param);
        return new ResponseEntity<>(agvTasksService.agvCallBack(param),HttpStatus.OK);
    }

    @AnonymousPostMapping("/agvCarLeave")
    @Log("AGV任务状态回调")
    @ApiOperation("AGV任务状态回调")
    public ResponseEntity<Object> agvCarLeave() {
        return new ResponseEntity<>(agvTasksService.agvCarLeave(),HttpStatus.OK);
    }

}